import {
  patientDscInfo,
  lastSign,
  loadEmrHistory,
  getEmrStatus,
  loadHistoricalVisitTree,
} from '@/api/modules/patient'

const state = {
  patientInfo: { id: '', time: '' },
  emrHistorys: { data: null, search: null, first: null },
  emrStatus: [],
  visitHistoricals: [],
  beforeEmrStatus: [],
  afterEmrStatus: [],
}

const mutations = {
  SET_PATIENT: (state, patientInfo) => {
    state.patientInfo = patientInfo
  },
  SET_HISTORY: (state, emrHistorys) => {
    state.emrHistorys = emrHistorys
  },
  SET_EMRSTATUS: (state, emrStatus) => {
    state.emrStatus = emrStatus
    state.beforeEmrStatus = emrStatus.slice(0, 3)
    state.afterEmrStatus = emrStatus.slice(3, 6)
  },
  SET_VISIHISTORICALS: (state, visitHistoricals) => {
    state.visitHistoricals = visitHistoricals
  },
}

const actions = {
  loadPatientInfo({ commit }, id) {
    return new Promise((resolve, reject) => {
      patientDscInfo({ id })
        .then(async (res) => {
          let { data } = await lastSign({ patientId: id }).catch(() => reject())
          const obj = { ...res.data, signs: data }

          resolve(obj)
          commit('SET_PATIENT', obj)
        })
        .catch((error) => {
          reject(error)
        })
    })
  },
  getEmrHistory({ commit, state }, config = {}) {
    return new Promise((resolve, reject) => {
      const dsc = state.emrHistorys.search
      if (!dsc && !config.search) {
        // (没有传入search store也没有)/config first为true则为初始化数据
        config.search = { pageIndex: 1, pageSize: 3, type: 0 }
      } else if (dsc && dsc.pageIndex && !config.search) {
        // 如不为空 则证明是累加数据
        config.search = {
          pageIndex: 1,
          pageSize: dsc.pageSize + 3,
          type: dsc.type,
        }
      }
      // 以上都没进if 说明传入了初始化数据
      loadEmrHistory({ patientId: config.pid, ...config.search })
        .then(async (res) => {
          const { data } = res
          const obj = { data, search: config.search }
          resolve(obj)
          commit('SET_HISTORY', obj)
        })
        .catch(() => {
          reject()
        })
    })
  },
  loadEmrStatus({ commit }, params) {
    const { medicalRecordId, patientId } = params
    return new Promise((resolve, reject) => {
      getEmrStatus({ medicalRecordId, patientId })
        .then((res) => {
          const items = [
            {
              id: 'screeningStatus',
              title: '筛查',
              status: 'wait',
              statusTitle: '未填写',
            },
            {
              id: 'medicalRecordStatus',
              title: '病历',
              status: 'wait',
              statusTitle: '未填写',
            },
            {
              id: 'evaluatelStatus',
              title: '评估',
              status: 'wait',
              statusTitle: '未填写',
            },
            {
              id: 'treatmentPlanstatus',
              title: '治疗方案',
              status: 'wait',
              statusTitle: '未保存',
            },
            {
              id: 'actionPlanStatus',
              title: '行动计划',
              status: 'wait',
              statusTitle: '未提交',
            },
            {
              id: 'pescriptionStatus',
              title: '处方',
              status: 'wait',
              statusTitle: '未填写',
            },
          ]
          // emrStatusArr.value = items
          const statusArr = items.map((item) => {
            // 存在且为true
            if (res.data && res.data[item.id]) {
              item.status = 'success'
              if (item.id === 'treatmentPlanstatus') {
                item.statusTitle = '已保存'
              } else if (item.id === 'actionPlanStatus') {
                item.statusTitle = '已提交'
              } else {
                item.statusTitle = '已填写'
              }
            }
            return item
          })
          commit('SET_EMRSTATUS', statusArr)
          resolve(statusArr)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
  loadHistoricalVisitInfo({ commit }, params) {
    return new Promise((resolve, reject) => {
      loadHistoricalVisitTree(params)
        .then((res) => {
          let v = []
          if (res.data) v = res.data
          const activities = v.reverse().map((item) => {
            const { id, createTime } = item.historicalVisitPatientDto
            const children = item.medicalRecordListDtos.map((item) => {
              item.pid = params.pid
              item.time = item.recordDate
              return item
            })
            return { id, time: createTime, label: '诊疗号：' + id, children }
          })

          commit('SET_VISIHISTORICALS', activities)
          resolve(activities)
        })
        .catch((err) => {
          reject(err)
        })
    })
  },
}

export default {
  namespaed: true,
  state,
  mutations,
  actions,
}
